package moodish.dummy;

import javax.swing.JOptionPane;

import moodish.client.MoodishClient;
import moodish.comm.ClientComm;
import moodish.comm.ClientSideMessage;
import moodish.ui.ClientInterface;

public class MoodishClientDummy implements MoodishClient {
	
	public ClientComm clientComm;
	public ClientInterface clientInterface;
	
	public class clientRunnable implements Runnable{
		@Override
		public void run() {
			try{
			while(true){
				ClientSideMessage msg = clientComm.getNextMessage();
				if(msg != null){
					switch (msg.getType())
					{
					case CONNECTED:
						if(!msg.getPayload().equals(ClientMainDummy.nickname.getText())){
							clientInterface.connectedClient(msg.getPayload());
						}
						break;
					case DISCONNECTED:
						if(!msg.getPayload().equals(ClientMainDummy.nickname.getText())){
							clientInterface.disconnectedClient(msg.getPayload());
						}
						break;
					case FRIENDSHIP:
						if(!msg.getPayload().equals(ClientMainDummy.nickname.getText())){
							clientInterface.friendship(msg.getPayload());
						}
						break;
					case MOODISH_MESSAGE:
						clientInterface.addMood(msg.getSendersNickname(), msg.getPayload());
						break;
					case UNFRIENDSHIP:
						if(!msg.getPayload().equals(ClientMainDummy.nickname.getText())){
							clientInterface.unfriendship(msg.getPayload());
						}
						//System.out.println("Client "+msg.getPayload()+" is not your friend anymore! :(");
						break;
					case ERROR:
						JOptionPane.showMessageDialog(null,
								msg.getPayload(),
								"ERROR!", JOptionPane.INFORMATION_MESSAGE);
						//System.out.println("ERROR: "+msg.getPayload());
						break;
					default:
						break;
					}
				} else {
					return;
				}
			}
			} catch (Exception e){
				return;
			}
		}
	}

	@Override
	public void start(ClientComm clientComm) {
		this.clientComm = clientComm;
		clientInterface = new ClientInterface(clientComm);
		
		Runnable clientTask = new clientRunnable();
	    Thread clientThread = new Thread(clientTask);
       	clientThread.start();
	}


}
